package gui;

import gamehandlers.GameHandler;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.util.ResourceBundle;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JSeparator;


/**
 * @author Yaqi Zhang
 * 
 */
public class GameFrame extends JFrame {
    private static final Dimension CANVAS_SIZE = new Dimension(500, 600);
    private ResourceBundle myResources;
    private Canvas myCanvas;
    private int myNumOfLevels;
    private GameHandler myGameHanlder;

    /**
     * @param title 
     * @param language 
     * @param numOfLevels 
     */
    public GameFrame (String title, String language, int numOfLevels, GameHandler gh) {
        setTitle(title);
        setPreferredSize(CANVAS_SIZE);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myResources = ResourceBundle.getBundle("resources." + language);
        myNumOfLevels = numOfLevels;
        makeMenus();
        setLayout(new GridBagLayout());
        setUpStart();
        pack();
        setVisible(true);
        myGameHanlder = gh;
    }

    private JComponent setUpCanvas () {
        clear();
        JComponent canvas = new Canvas(CANVAS_SIZE, myGameHanlder);
        getContentPane().add(canvas);
        repaint();
        return canvas;
    }

    private void setUpStart () {
        clear();
        AbstractAction aa = new AbstractAction() {
            @Override
            public void actionPerformed (ActionEvent e) {
                setUpChooseLevel();
            }
        };
        JButton b = new JButton(aa);
        b.setText(myResources.getString("PlayCommand"));
        makeDisplay(b);
    }

    private void setUpChooseLevel () {
        clear();
        for (int i = 0; i < myNumOfLevels; i++) {
            AbstractAction aa = new AbstractAction() {
                @Override
                public void actionPerformed (ActionEvent e) {
                    setUpCanvas();
                }
            };
            JButton b = new JButton(aa);
            b.setText(myResources.getString("LevelCommand") + " " + i);
            makeDisplay(b);
        }
    }

    private void clear () {
        getContentPane().removeAll();
    }

    private void makeDisplay (JButton ... buttons) {
        for (JButton bt : buttons) {
            getContentPane().add(bt, new GridBagConstraints());
        }
        pack();
    }

    /**
     * Call this when the player loses
     */
    public void lose () {
        setUpEnd();
    }

    /**
     * Go to the next level or finish the game.
     * 
     */
    public void nextLevel () {
        setUpEnd();
    }

    private void setUpEnd () {
        AbstractAction aa1 = new AbstractAction() {
            @Override
            public void actionPerformed (ActionEvent e) {
                setUpCanvas();
            }
        };
        JButton b1 = new JButton(myResources.getString("ReplayCommand"));
        b1.setAction(aa1);
        AbstractAction aa2 = new AbstractAction() {
            @Override
            public void actionPerformed (ActionEvent e) {
                System.exit(0);
            }
        };
        JButton b2 = new JButton(myResources.getString("ReplayCommand"));
        b1.setAction(aa2);
        makeDisplay(b1, b2);
    }

    private JMenuBar makeMenus () {
        JMenuBar bar = new JMenuBar();
        bar.add(makeFileMenu());
        setJMenuBar(bar);
        return bar;
    }

    @SuppressWarnings("serial")
    private JMenu makeFileMenu () {
        JMenu result = new JMenu(myResources.getString("FileMenu"));
        result.add(new AbstractAction(myResources.getString("NewGameCommand")) {
            @Override
            public void actionPerformed (ActionEvent e) {
                // go to start page
            }
        });
        result.add(new JSeparator());
        result.add(new AbstractAction(myResources.getString("QuitCommand")) {
            @Override
            public void actionPerformed (ActionEvent e) {
                // end program
                // System.exit(0);
            }
        });
        return result;
    }

    /**
     * @return the key pressed by the player.
     */
    public int getUserKeyInput () {
        return myCanvas.getUserKeyInput();
    }

    /**
     * @return the canvas size
     */
    public Dimension getCanvasSize () {
        return CANVAS_SIZE;
    }

    /**
     * 
     * @param args 
     */
    public static void main (String[] args) {
        new GameFrame("a", "English", 3, null);
    }
}
